package xyz.lne.researchgroupmanage.security;

import com.google.common.collect.ImmutableMap;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import xyz.lne.researchgroupmanage.exception.BadRequestException;
import xyz.lne.researchgroupmanage.exception.UserPasswordException;
import xyz.lne.researchgroupmanage.service.ResearchGroupService;
import xyz.lne.researchgroupmanage.service.ResearchService;
import xyz.lne.researchgroupmanage.service.SysUserService;
import xyz.lne.researchgroupmanage.service.TaskService;
import xyz.lne.researchgroupmanage.service.dataTransferObject.JwtUserDTO;
import xyz.lne.researchgroupmanage.service.dataTransferObject.UserDTO;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Designed By Lne.
 * 2021/1/27 20:23
 */
@RequiredArgsConstructor
@Service("UserDetailsService")
public class UserDetailsServiceImpl implements UserDetailsService {
    private final SysUserService sysUserService;
    private final ResearchService researchService;
    private final ResearchGroupService researchGroupService;
    private final TaskService taskService;

    static Map<String, JwtUserDTO> jwtUserDTOCache = new ConcurrentHashMap<>();

    @Override
    public UserDetails loadUserByUsername(String account) throws UsernameNotFoundException {
        JwtUserDTO jwtUserDTO = null;
        /*
        if (jwtUserDTOCache.containsKey(account)){
            jwtUserDTO = jwtUserDTOCache.get(account);
        }else {
            UserDTO userDTO = sysUserService.findUserByAccount(account);
            if (userDTO == null){
                throw new UserPasswordException(ImmutableMap.of("账号:", account));
            }
            jwtUserDTO = new JwtUserDTO(userDTO, researchGroupService.findCreatedResearchGroupByUserId(userDTO.getUserId()),
                    researchGroupService.findJoinedResearchGroupByUserId(userDTO.getUserId()),
                    researchService.findResearchBymanagerId(userDTO.getUserId()), researchService.findResearchByuserId(userDTO.getUserId()),
                    taskService.getParticipatedTaskListByUserId(userDTO.getUserId()));
            jwtUserDTOCache.put(account, jwtUserDTO);
        }*/
        UserDTO userDTO = sysUserService.findUserByAccount(account);
        if (userDTO == null){
            throw new UserPasswordException(ImmutableMap.of("账号:", account));
        }
        jwtUserDTO = new JwtUserDTO(userDTO, researchGroupService.findCreatedResearchGroupByUserId(userDTO.getUserId()),
                researchGroupService.findJoinedResearchGroupByUserId(userDTO.getUserId()),
                researchService.findResearchBymanagerId(userDTO.getUserId()), researchService.findResearchByuserId(userDTO.getUserId()),
                taskService.getParticipatedTaskListByUserId(userDTO.getUserId()));
        jwtUserDTOCache.put(account, jwtUserDTO);
        return jwtUserDTO;
    }

    public static UserDetails loadUserInCache(String account){
        UserDetails userDetails = jwtUserDTOCache.get(account);
        if (userDetails == null)
            throw new BadRequestException(ImmutableMap.of("安全错误", "当前登录状态过期"));
        return userDetails;
    }

    public static boolean removeUserInCache(String account){
        jwtUserDTOCache.remove(account);
        return true;
    }


}
